package com.xilihui.xlh.business.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.AppointmentEntity;
import com.xilihui.xlh.business.entities.PayingEntity;
import com.xilihui.xlh.business.requests.MyAppointmentRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.constant.StrConst;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.glide.ImageHelper;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.TextStringUtils;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.feezu.liuli.timeselector.Utils.TextUtil;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/6/1.
 */

public class MergePaymentsActivity extends BaseCompatActivity{
    
    public static final String DELETE = "cexiao";
    
    @BindView(R.id.tv_orderName)
    TextView tv_orderName;
    @BindView(R.id.iv_paying)
    ImageView iv_pay;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.tv_cexiao)
    TextView tv_cexiao;
    
    String wa_ids = "";
    String id = "";//收款正在进行id
    String url = "";
    
    ArrayList<AppointmentEntity.DataBean.AppointBean> data = new ArrayList<>();
    BaseAdapter<AppointmentEntity.DataBean.AppointBean> baseAdapter;
    
    
    
    @Override
    public int getLayout() {
        return R.layout.activity_merge_payments;
    }

    @Override
    public String initTitle() {
        return "扫码付款";
    }

   

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        
    }

    @Override
    public void initData() {
        wa_ids = getIntent().getStringExtra("wa_ids");
        id = getIntent().getStringExtra("id");
        LogUtil.i("mylog","ffffid:"+id);
        if(!TextUtil.isEmpty(id)){
            getData();
            tv_cexiao.setVisibility(View.VISIBLE);
            url = "api/appoint/paying_sa_code?id=";
            
        }else {
            url = "api/appoint/paying_code?wa_ids=";
            data = (ArrayList<AppointmentEntity.DataBean.AppointBean>) getIntent().getSerializableExtra("data");
            handelData();
        }
        

        baseAdapter =
                new BaseAdapter<AppointmentEntity.DataBean.AppointBean>(this,data) {
                    @Override
                    protected int setLayoutId() {
                        return R.layout.recycler_item_reservation_list;
                    }

                    @Override
                    protected void getView(BaseViewHolder holder,AppointmentEntity.DataBean.AppointBean entity, int position) {
                        
                        holder.setText(R.id.tv_title,"预约单"+(position+1));
                        
                        TextView tv_xianmu = (TextView)holder.getView(R.id.tv_xianmu);
                        String xianmu = entity.getHair_project().getName();
                        String str = "预约项目："+xianmu;
                        TextStringUtils.setSpanTextView(tv_xianmu,str,new TextStringUtils.SpanColorHolder(
                                str.length()-xianmu.length(),str.length(),tv_xianmu.getContext().getResources()
                                .getColor(R.color.tv_333333)
                        ));
                        
                        TextView tv_zuoweiType = (TextView)holder.getView(R.id.tv_zuoweiType);
                        String type = entity.getSeat().getName();
                        String str2 = "座位类型："+type;
                        TextStringUtils.setSpanTextView(tv_zuoweiType,str2,new TextStringUtils.SpanColorHolder(
                                str2.length()-type.length(),str2.length(),tv_zuoweiType.getContext().getResources()
                                .getColor(R.color.tv_333333)
                        ));
                        
                        holder.setText(R.id.tv_beizhu,entity.getRemark());
                        SimpleDraweeView iv = (SimpleDraweeView)holder.getView(R.id.civ_head);
                        iv.setImageURI(UrlConst.baseUrl()+entity.getAgent().getHeadimg());
                        
                    }
                };
        
        baseAdapter.setAnimationsLocked(true);
        recyclerView.setAdapter(baseAdapter);
        recyclerView.setFocusable(false);
        
    }
    
    public void handelData(){
        
        for(int i=0;i<data.size();i++){
            wa_ids = wa_ids+data.get(i).getId()+",";
        }
        if(!TextUtil.isEmpty(wa_ids)){
            wa_ids = wa_ids.substring(0,wa_ids.length()-1);
        }

        tv_orderName.setText("已生成"+data.size()+"个预约单,请用户扫码付款");

        String pay_url = UrlConst.baseUrl()+url+wa_ids;
        LogUtil.i("mylog",pay_url+"  "+wa_ids);
        ImageHelper.display(this,iv_pay,pay_url);
    }
    
    public void getData(){
        
        MyAppointmentRequest.paying(MergePaymentsActivity.this,id)
                .compose(DoTransform.<PayingEntity>applyScheduler(this,true))
                .compose(MergePaymentsActivity.this.<PayingEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<PayingEntity>(this) {
                    @Override
                    protected void onSuccess(PayingEntity entity) {
                        data = (ArrayList<AppointmentEntity.DataBean.AppointBean>) entity.getData();
                        if(data!=null){
                            String pay_url = UrlConst.baseUrl()+url+id;
                            ImageHelper.display(MergePaymentsActivity.this,iv_pay,pay_url);
                            tv_orderName.setText("已生成"+data.size()+"个预约单,请用户扫码付款");
                        }
                        baseAdapter.setList(data);
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                    
                });
        
    }
    
    @OnClick(R.id.tv_cexiao)
    public void cexiao(){
        
        MyAppointmentRequest.deletePaying(MergePaymentsActivity.this,id)
                .compose(DoTransform.<BaseEntity>applyScheduler(this,true))
                .compose(this.<BaseEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<BaseEntity>(this) {
                    @Override
                    protected void onSuccess(BaseEntity entity) {
                        ToastUtil.toastShortPositive(entity.getMsg());
                        YEventBuses.post(new YEventBuses.Event(DELETE));
                        finish();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
        
    }

    @Subscribe
    public void onEventMainThread(YEventBuses.Event event) {
        super.onEventMainThread(event);
        if(event.is(StrConst.UMENG_PUSH)){
            int push_status = Integer.valueOf((String) event.getParams(0));
            LogUtil.i("mylog"," push_status:"+push_status);
            switch (push_status){
                case 1:  //开始
                    ToastUtil.toastLongPositive("付款成功");
                    tv_cexiao.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Intent intent = new Intent(MergePaymentsActivity.this,HistoryRecordActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    },1000);
                case 2:  
                                    
                    break;
              
            }

        }

    }
    
}
